Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
React: "toISOString is not a function"

I am coding a Todo List with Next.js. I want to implement a dueDate function, if I make the post request, I get this error:

Unhandled Runtime Error
TypeError: dueDate.toISOString is not a function

I dont know how I can fix this, it would be nice if anyone can help me :)

My code:

const [newTodo, setNewTodo] = useState<any>({
    title: "",
    description: "",
})
const [dueDate, setDueDate] = useState<Date>(new Date())
const addTodo = () => {
    axios.post("http://localhost:5000/todos", {
        title: newTodo.title,
        description: newTodo.description,
        dueDate: dueDate.toISOString().slice(0, 10)
    }).then(res => {
        setTodos([...todos, res.data])
        setNewTodo({
            title: "",
            description: "",
            dueDate: "",
        })
    })
}

    
<input className="dueDate" type="date" onChange={(e) => setDueDate(e.target.value)} />
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem is that input calls setDueDate passing e.target.value that is a string and not a Date, and you know that:

The "toISOString is not a function" error occurs when the toISOString() method is called on a value that is not a date object.

To solve this it's just necessary to modify input's onChange:

<input className="dueDate" type="date" onChange={(e) => setDueDate(new Date(e.target.value))} />

For more info

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!